﻿using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKCodeBase.WinForm;
using YJKView;
using ModelChecking.ModelValidation.Walls;

namespace ModelChecking.ModelValidation.Commands
{
	[Regeneration(RegenerationOption.Manual)]
	[Transaction(TransactionMode.Manual)]
	public class CmdWallsValidation : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData cmdData, ref string msg, ElementSet elems)
		{
			FloorSelectManager.CommandName = "CmdWallsValidation";
			CmdViewType types = CmdViewType.FloorPlan;
            if (SwitchView.SetUsableView(types, cmdData) != Autodesk.Revit.UI.Result.Succeeded)
			{
				return Autodesk.Revit.UI.Result.Failed;
			}
			if (CmdWallsValidation._hWndRevit == null)
			{
				Process currentProcess = Process.GetCurrentProcess();
				IntPtr mainWindowHandle = currentProcess.MainWindowHandle;
				CmdWallsValidation._hWndRevit = new WindowHandle(mainWindowHandle);
			}
			WallsValidationManager wallsValidationManager = new WallsValidationManager(cmdData);
			Result result = wallsValidationManager.Validation(ref msg);
			if (result != Result.Succeeded)
			{
				return result;
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		public static WindowHandle _hWndRevit;
	}
}
